/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2016-11-12     Bernard      The first version
 * 2018-05-25     armink       Add simple API, such as LOG_D, LOG_E
 * 2019-03-12	  zibuyu	   Add Jlink zhichi
 * 2020-05-29	  zibuyu	   Add event recorder zhichi
 */

/*
 * The macro definitions for debug
 *
 * These macros are defined in static. If you want to use debug macro, you can
 * use as following code:
 *
 * In your C/C++ file, enable/disable DEBUG_ENABLE macro, and then include this
 * header file.
 *
 * #define DBG_SECTION_NAME    "MOD"
 * #define DBG_ENABLE          // enable debug macro
 * #define DBG_LEVEL           DBG_INFO
 * #include <rtdbg.h>          // must after of DEBUG_ENABLE or some other options
 *
 * Then in your C/C++ file, you can use LOG_X macro to print out logs:
 * LOG_D("this is a debug log!");
 * LOG_E("this is a error log!");
 *
 * If you want to use different color for different kinds log, you can
 * #define DBG_COLOR
 */

#ifndef RT_DBG_H__
#define RT_DBG_H__

//#define RTDBG_EVENT_RECORDER
//#define RTDBG_JLINK_RTT
//#define RTDBG_USART

#ifdef RTDBG_EVENT_RECORDER
	#include "EventRecorder.h"
	#include "stdio.h"
	
	#define rtdbg_init() 				EventRecorderInitialize(EventRecordAll, 1U);EventRecorderStart()
	#define RT_GET_KEY()				-1
	#define rt_kprintf					printf
	
	#ifdef DBG_COLOR
		#undef DBG_COLOR
	#endif

#elif defined( RTDBG_USART )
	extern void  mine_printf( const char*fmt, ... );
	extern short mine_read_data( void );
	extern void  mine_printf_init(void);
	
	#define rtdbg_init()				mine_printf_init()
	#define RT_GET_KEY()				mine_read_data()
	#define rt_kprintf(format, ...) 	mine_printf (format, ##__VA_ARGS__)
#elif defined( RTDBG_JLINK_RTT )
	#include "SEGGER_RTT.h"
	#define rtdbg_init()				SEGGER_RTT_Init(); SEGGER_RTT_WriteString(0, RTT_CTRL_CLEAR)
	#define RT_GET_KEY()				SEGGER_RTT_GetKey()	
	#define rt_kprintf(format, ...) 	SEGGER_RTT_printf( 0, format, ##__VA_ARGS__)		
#else
	#define rtdbg_init()
	#define RT_GET_KEY()				-1
	#define rt_kprintf(format, ...)
#endif
	
	
	
	
#ifdef RTDBG_JLINK_RTT
#define jlink_rtt_en
#endif

	
	
	



#if defined(RT_USING_ULOG) && defined(DBG_ENABLE)
/* using ulog compatible with rtdbg  */
#include <ulog.h>

#else
/* DEBUG level */
#define DBG_ERROR           0
#define DBG_WARNING         1
#define DBG_INFO            2
#define DBG_LOG             3

#ifndef DBG_SECTION_NAME
#define DBG_SECTION_NAME    "DBG"
#endif


#ifdef DBG_ENABLE

#ifndef DBG_LEVEL
#define DBG_LEVEL         DBG_WARNING
#endif

/*
 * The color for terminal (foreground)
 * BLACK    30
 * RED      31
 * GREEN    32
 * YELLOW   33
 * BLUE     34
 * PURPLE   35
 * CYAN     36
 * WHITE    37
 */ 

#ifdef jlink_rtt_en /* 定义 jlink，两种颜色格式不一样 */

	#ifdef DBG_COLOR
	#define _DBG_COLOR(n)        rt_kprintf("\x1B[1;"#n"m")
	#define _DBG_LOG_HDR(lvl_name, color_n)                    \
		rt_kprintf("\x1B[1;"#color_n"m[" lvl_name "/" DBG_SECTION_NAME "] ")
	#define _DBG_LOG_X_END                                     \
		rt_kprintf("\x1B[1;0m\n")
	#else
	#define _DBG_COLOR(n)
	#define _DBG_LOG_HDR(lvl_name, color_n)                    \
		rt_kprintf("["lvl_name"/"DBG_SECTION_NAME"] ")
	#define _DBG_LOG_X_END                                     \
		rt_kprintf("\n")
	#endif /* DBG_COLOR */

#else

	#ifdef DBG_COLOR
	#define _DBG_COLOR(n)        rt_kprintf("\033["#n"m")
	#define _DBG_LOG_HDR(lvl_name, color_n)                    \
		rt_kprintf("\033[" #color_n "m[" lvl_name "/" DBG_SECTION_NAME "] ")
	#define _DBG_LOG_X_END                                     \
		rt_kprintf("\033[0m\r\n")
	#else
	#define _DBG_COLOR(n)
	#define _DBG_LOG_HDR(lvl_name, color_n)                    \
		rt_kprintf("["lvl_name"/"DBG_SECTION_NAME"] ")
	#define _DBG_LOG_X_END                                     \
		rt_kprintf("\r\n")
	#endif /* DBG_COLOR */

#endif

/*
 * static debug routine
 * NOTE: This is a NOT RECOMMENDED API. Please using LOG_X API.
 *       It will be DISCARDED later. Because it will take up more resources.
 */
#define dbg_log(level, fmt, ...)                            \
    if ((level) <= DBG_LEVEL)                               \
    {                                                       \
        switch(level)                                       \
        {                                                   \
            case DBG_ERROR:   _DBG_LOG_HDR("E", 31); break; \
            case DBG_WARNING: _DBG_LOG_HDR("W", 33); break; \
            case DBG_INFO:    _DBG_LOG_HDR("I", 32); break; \
            case DBG_LOG:     _DBG_LOG_HDR("D", 0); break;  \
            default: break;                                 \
        }                                                   \
        rt_kprintf(fmt, ##__VA_ARGS__);                     \
        _DBG_COLOR(0);                                      \
    }

#define dbg_here                                            \
    if ((DBG_LEVEL) <= DBG_LOG){                            \
        rt_kprintf(DBG_SECTION_NAME " Here %s:%d\n",        \
            __FUNCTION__, __LINE__);                        \
    }

#define dbg_enter                                           \
    if ((DBG_LEVEL) <= DBG_LOG){                            \
        _DBG_COLOR(32);                                     \
        rt_kprintf(DBG_SECTION_NAME " Enter %s\n",          \
            __FUNCTION__);                                  \
        _DBG_COLOR(0);                                      \
    }

#define dbg_exit                                            \
    if ((DBG_LEVEL) <= DBG_LOG){                            \
        _DBG_COLOR(32);                                     \
        rt_kprintf(DBG_SECTION_NAME " Exit  %s:%d\n",       \
            __FUNCTION__);                                  \
        _DBG_COLOR(0);                                      \
    }


#define dbg_log_line(lvl, color_n, fmt, ...)                \
    do                                                      \
    {                                                       \
        _DBG_LOG_HDR(lvl, color_n);                         \
        rt_kprintf(fmt, ##__VA_ARGS__);                     \
        _DBG_LOG_X_END;                                     \
    }                                                       \
    while (0)

#define dbg_raw(...)         rt_kprintf(__VA_ARGS__);

#else
#define dbg_log(level, fmt, ...)
#define dbg_here
#define dbg_enter
#define dbg_exit
#define dbg_log_line(lvl, color_n, fmt, ...)
#define dbg_raw(...)
#endif /* DBG_ENABLE */

#if (DBG_LEVEL >= DBG_LOG)
#define LOG_D(fmt, ...)      dbg_log_line("D", 0, fmt, ##__VA_ARGS__)
#else
#define LOG_D(...)
#endif

#if (DBG_LEVEL >= DBG_INFO)
#define LOG_I(fmt, ...)      dbg_log_line("I", 32, fmt, ##__VA_ARGS__)
#else
#define LOG_I(...)
#endif

#if (DBG_LEVEL >= DBG_WARNING)
#define LOG_W(fmt, ...)      dbg_log_line("W", 33, fmt, ##__VA_ARGS__)
#else
#define LOG_W(...)
#endif

#if (DBG_LEVEL >= DBG_ERROR)
#define LOG_E(fmt, ...)      dbg_log_line("E", 31, fmt, ##__VA_ARGS__)
#else
#define LOG_E(...)
#endif

#define LOG_RAW(...)         dbg_raw(__VA_ARGS__)

#endif /* defined(RT_USING_ULOG) && define(DBG_ENABLE) */

#endif /* RT_DBG_H__ */








